﻿using pingjia.bll;
using PJTY.dal;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class views_admin_Default : System.Web.UI.Page
{
    private ConsumerService _consumerService = new ConsumerService();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username"] != null)
            {
                string currentUserName = Session["username"].ToString();
                LinqDataSource2.WhereParameters.Add("ConsumerName", DbType.String, currentUserName);

                var consumer = _consumerService.GetConsumerInformation(currentUserName);
                if (consumer != null)
                {
                    TextBox2.Text = consumer.ConsumerID.ToString();
                    TextBox3.Text = consumer.ConsumerName;
                    TextBox4.Text = consumer.ConsumerAge.ToString();
                }
            }
        }
    }

    protected void Unnamed2_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            _consumerService.Review(
                int.Parse(TextBox1.Text.Trim()),
                int.Parse(TextBox2.Text.Trim()),
                TextBox3.Text.Trim(),
                TextBoxname.Text.Trim(),
                int.Parse(TextBox4.Text.Trim()),
                TextBox5.Text.Trim(),
                int.Parse(TextBox6.Text.Trim()),
                TextBox7.Text.Trim()
            );

            ShowAlert("提交成功！");
        }
    }

    protected void Unnamed1_Click(object sender, EventArgs e)
    {
        ClearTextBoxes();
    }

    protected void Unnamed3_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string name = FileUpload1.FileName;
            string type = name.Substring(name.LastIndexOf(".") + 1).ToLower();
            if (IsImageFile(type))
            {
                string ipath = Server.MapPath("Image") + "//" + name;
                string wpath = "Image//" + name;
                FileUpload1.SaveAs(ipath);
                _consumerService.SaveImagePath(int.Parse(TextBox1.Text.Trim()), wpath);
                ShowAlert("保存成功！");
            }
            else
            {
                ShowAlert("该文件不是图片格式！");
            }
        }
        else
        {
            ShowAlert("请选择要上传的图片");
        }
    }

    protected void rptProducts2_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string trialID = ((Label)e.Item.FindControl("TrialID")).Text;
        string productName = ((Label)e.Item.FindControl("ProductName")).Text;
        string trialStatus = ((Label)e.Item.FindControl("TrialStatus")).Text;

        if (trialStatus == "已发货")
        {
            TextBox1.Text = trialID;
            TextBoxname.Text = productName;
        }
        else if (trialStatus == "申请中")
        {
            ShowAlert("审核尚未通过");
        }
        else
        {
            ShowAlert("商品尚未发货");
        }
    }

    private void ClearTextBoxes()
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        TextBox3.Text = "";
        TextBox4.Text = "";
        TextBox5.Text = "";
        TextBox6.Text = "";
        TextBox7.Text = "";
    }

    private void ShowAlert(string message)
    {
        string script = string.Format("alert('{0}');", message);
        ClientScript.RegisterStartupScript(this.GetType(), "AlertMessage", script, true);
    }

    private bool IsImageFile(string fileType)
    {
        string[] validTypes = { "jpg", "jpeg", "gif", "bmp", "png" };
        return Array.IndexOf(validTypes, fileType) >= 0;
    }
}
               
    
